package com.example.testdemo.controller;

import com.example.testdemo.dto.User;
import com.example.testdemo.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
/**
 * @Author: chengxuefei
 * @Date: 2021/7/2 0026
 * @Time: 15:23
 */
@Controller
@Api(value = "测试用户信息")
public class UserController {

    @Resource
    UserService userService;

    @ApiOperation(value = "获取用户信息", notes = "通过用户ID获取用户信息")
    @GetMapping("/test/{id}")
    @ResponseBody
    public User getUser(@PathVariable(name = "id") String id){
        return userService.findById(id);

    }
    @ApiOperation(value = "添加用户信息", notes = "添加用户的信息")
    @PostMapping("/test")
    @ResponseBody
    public String add(@RequestBody User user){
        userService.add(user);
        return "添加成功";

    }
    @ApiOperation(value = "修改用户信息", notes = "修改用户的信息")
    @PutMapping("/test/{id}")
    @ResponseBody
    public String update(@PathVariable(name = "id")String id , @RequestBody User user){
        User user1 = userService.findById(id);
        if (user1!=null){
            userService.add(user);
            return "修改成功";
        }else {
            System.out.println(543645);
            return "id不存在";
        }
    }
    @ApiOperation(value = "删除用户信息", notes = "删除用户信息")
    @DeleteMapping("/test/{id}")
    @ResponseBody
    public String delete(@PathVariable(name = "id")String id ){
        User user1 = userService.findById(id);
        if (user1!=null){
            userService.delete(user1);
            return "删除成功";
        }else {
            return "id不存在";
        }
    }
    @ApiOperation(value = "分页查询用户信息", notes = "分页查询用户信息")
    @GetMapping("/test/list/page")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数",  dataType = "Integer")
    })
    public Page<User> findListPage(Integer page,Integer size){

        return userService.findListPage(page, size);

    }

    @ApiOperation(value = "查询存储过程", notes = "查询存储过程(输入的结果值加2)")
    @GetMapping("/test/fun")
    @ResponseBody
    public Long testFun(Integer adc){
        Long testfun = userService.testfun(adc);
        return testfun;
    }
    @ApiOperation(value = "查询函数", notes = "查询函数")
    @GetMapping("/test/ABS")
    @ResponseBody
    public Long testABS(Integer adc){
        Long testfun = userService.testABS(adc);
        return testfun;
    }
}
